package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;


import util.CommonSql;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");//防止post乱码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        System.out.println("user="+user.getUsername());
        System.out.println("pswd="+user.getPassword());
        String sql;
        sql = " SELECT * FROM user where username = '"+user.getUsername()+"' and password = '"+user.getPassword()+"'";
        CommonSql login = new CommonSql();
        User user1 = login.CaozuoSql(sql);
        //user.getUsername().equals(user1.getUsername()) && user.getPassword().equals(user1.getPassword())
        if(user1 != null){
            System.out.println("查找成功");
            request.setAttribute("usertemp", user1);
            request.getRequestDispatcher("/main.jsp").forward(request, response);
        }
        else{
            System.out.println("查找失败");
            request.setAttribute("errInfo", "用户名或密码不正确！");

            String message = "登陆失败，请返回重新登录";
            request.setAttribute("message",message);

            request.getRequestDispatcher("/Login.jsp").forward(request, response);
        }



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // System.out.println("tt");
        doGet(request,response);
    }
}

